<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Java 字符串常量池</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 4s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.8; }
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 15px;
            font-size: 12px;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::after {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 70%);
            animation: rotate 10s linear infinite;
        }
        
        @keyframes rotate {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
        
        .feature-card {
            background: white;
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            border: 1px solid rgba(102, 126, 234, 0.1);
        }
        
        .feature-card:hover {
            transform: translateY(-10px);
            box-shadow: 0 20px 40px rgba(102, 126, 234, 0.2);
            border-color: rgba(102, 126, 234, 0.3);
        }
        
        .icon-wrapper {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 20px;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 30px;
            bottom: -30px;
            width: 2px;
            background: linear-gradient(to bottom, #667eea, transparent);
        }
        
        .timeline-item:last-child::before {
            display: none;
        }
        
        .timeline-dot {
            position: absolute;
            left: 0;
            top: 8px;
            width: 20px;
            height: 20px;
            background: white;
            border: 3px solid #667eea;
            border-radius: 50%;
        }
        
        .mermaid {
            background: white;
            padding: 30px;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0.1em 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto relative z-10">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                    深入理解 Java 字符串常量池
                </h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                    探索 Java 内存管理的核心机制，揭秘字符串优化的奥秘
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <i class="fas fa-memory text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">内存优化</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-tachometer-alt text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">性能提升</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-code text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">最佳实践</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-16">
        <!-- Introduction -->
        <div class="content-card rounded-2xl p-10 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">字</span>符串常量池（String Constant Pool）是 Java 内存管理的一部分，用于存储字符串字面量，以提高内存利用率和性能。在 Java 中，字符串是不可变的，因此 Java 设计了一种机制来避免在内存中创建多个相同内容的字符串对象，这就是字符串常量池的作用。
            </p>
        </div>

        <!-- Core Concepts -->
        <div class="grid md:grid-cols-2 gap-8 mb-16">
            <div class="feature-card">
                <div class="icon-wrapper">
                    <i class="fas fa-database text-white text-2xl"></i>
                </div>
                <h3 class="text-2xl font-bold mb-4 text-gray-800">什么是字符串常量池？</h3>
                <p class="text-gray-600 leading-relaxed">
                    字符串常量池是 Java 堆内存中的一块特殊区域，用来存储字符串字面量。每当一个字符串字面量被创建时，Java 会先检查该字符串是否已经存在于常量池中。
                </p>
            </div>
            
            <div class="feature-card">
                <div class="icon-wrapper">
                    <i class="fas fa-cogs text-white text-2xl"></i>
                </div>
                <h3 class="text-2xl font-bold mb-4 text-gray-800">工作原理</h3>
                <p class="text-gray-600 leading-relaxed">
                    如果字符串存在于池中，直接返回对该池中已有字符串的引用；如果不存在，则会将这个新的字符串添加到常量池中，然后返回引用。
                </p>
            </div>
        </div>

        <!-- Working Mechanism -->
        <div class="mb-16">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-gear text-purple-600 mr-3"></i>工作机制详解
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 rounded-2xl p-8">
                    <h4 class="text-xl font-bold mb-4 text-gray-800">
                        <i class="fas fa-quote-left text-purple-600 mr-2"></i>字面量字符串
                    </h4>
                    <p class="text-gray-700 mb-4">
                        当你通过字面量的方式创建字符串时，如 <code class="bg-gray-800 text-white px-2 py-1 rounded">String s1 = "hello";</code>，Java 会先检查常量池中是否已有 "hello"。
                    </p>
                    <ul class="space-y-2 text-gray-600">
                        <li><i class="fas fa-check text-green-500 mr-2"></i>如果有，直接返回引用</li>
                        <li><i class="fas fa-check text-green-500 mr-2"></i>如果没有，存入常量池后返回引用</li>
                    </ul>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-indigo-50 rounded-2xl p-8">
                    <h4 class="text-xl font-bold mb-4 text-gray-800">
                        <i class="fas fa-code text-blue-600 mr-2"></i>intern() 方法
                    </h4>
                    <p class="text-gray-700 mb-4">
                        对于通过 <code class="bg-gray-800 text-white px-2 py-1 rounded">new</code> 关键字创建的字符串对象，可以通过调用 <code class="bg-gray-800 text-white px-2 py-1 rounded">intern()</code> 方法将其手动加入到字符串常量池。
                    </p>
                    <p class="text-sm text-gray-600">
                        例如：<code class="bg-gray-800 text-white px-2 py-1 rounded text-sm">String s2 = new String("world").intern();</code>
                    </p>
                </div>
            </div>
        </div>

        <!-- Benefits -->
        <div class="highlight-box mb-16">
            <h2 class="text-3xl font-bold mb-6 relative z-10">
                <i class="fas fa-star mr-3"></i>为什么使用字符串常量池？
            </h2>
            <div class="grid md:grid-cols-2 gap-8 relative z-10">
                <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-lg rounded-xl p-6">
                    <h4 class="text-xl font-bold mb-3">
                        <i class="fas fa-save mr-2"></i>节省内存
                    </h4>
                    <p>避免在内存中创建重复的字符串对象。例如，字符串 "hello" 如果在多个地方被使用，通过字符串常量池，只需要存储一个实例。</p>
                </div>
                <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-lg rounded-xl p-6">
                    <h4 class="text-xl font-bold mb-3">
                        <i class="fas fa-rocket mr-2"></i>提高性能
                    </h4>
                    <p>通过字符串常量池，字符串的比较操作可以直接比较引用（使用 == 操作符），而不需要逐字符比较。</p>
                </div>
            </div>
        </div>

        <!-- Evolution Timeline -->
        <div class="mb-16">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-history text-purple-600 mr-3"></i>字符串常量池的演进
            </h2>
            
            <div class="content-card rounded-2xl p-10">
                <div class="timeline-item mb-8">
                    <div class="timeline